<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/25 0025
 * Time: 8:37
 */

namespace app\service;

use phpmailer\BaseException;
use think\Db;

/**
 * 基础服务层
 * @author   juzi
 * @blog    https://blog.csdn.net/juziaixiao
 * @version  0.0.1
 * @datetime 2020年7月22日10:57:35
 */
class BaseService
{
    /**
     * 获取数据
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version  0.0.1
     * @datetime 2020年7月22日10:57:35
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
    public static function DataList($params = [])
    {
        $where = empty($params['where']) ? [['id','>',0]] : $params['where'];
        $page = $params['page'] ? true : false;
        $number = isset($params['number']) ? intval($params['number']) : 10;
        $field = empty($params['field']) ? '*' : $params['field'];
        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 0;
        // 获取列表
        $data = Db::name($params['table'])->where($where)->field($field)->order('create_time desc');
        //分页
        if($page)
        {
            $data=$data->paginate($number, false, ['query' => request()->param()]);
            $data=$data->toArray()['data'];
        }else{
            if($n){
                $data=$data->limit($m, $n)->select();
            }else{
                $data=$data->select();
            }
        }

        return $data;

    }

    /**
     * 总数
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日13:29:33
     * @desc    description
     * @param    [array]          $where [条件]
     */
    public static function BannerTotal($table,$where){


        return (int) Db::name($table)->where($where)->count();
    }

    /**
     * [删除数据].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日17:02:36
     */
    public static function DelInfo($params)
    {

        //判断是否是等级分类
        $id = $params['id'];
        $id = explode(',', $id);
        $where_del = [['id' ,'in', $id]];

        Db::startTrans();
        if($params['soft'])
        {
            $re= Db::name($params['table'])->where($where_del)->update($params['soft_arr']);
        }else{
            $re= Db::name($params['table'])->where($where_del)->delete();
        }
        if (!$re)
        {
            Db::rollback();
            throw new BaseException(['code'=>403 ,'errorCode'=>$params['errorcode'],'msg'=>$params['msg'],'status'=>false,'debug'=>false]);
        }
        Db::commit();
         return true;
    }


    /**
     * 总数
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日13:29:33
     * @desc    description
     * @param    [array]          $where [条件]
     */
    public static function DataTotal($table,$where){
        return (int) Db::name($table)->where($where)->count();
    }



    /**
     * 分类保存
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月21日15:25:02
     * @param    [array]          $params [输入参数]
     */
    public static  function SaveInfo($data,$params){

        // 添加/编辑
        if (empty($data['id']))
        {
            $cate_id = Db::name($params['table'])->insertGetId($data);
            if (!$cate_id) {

                throw new BaseException(['code'=>403 ,'errorCode'=>'30006','msg'=>'添加失败','status'=>false,'debug'=>false]);
            }
        } else {
            $data['update_time'] = TIMESTAMP;
            $re = Db::name($params['table'])->where(['id' => intval($data['id'])])->update($data);
            if (!$re)
            {
                throw new BaseException(['code'=>403 ,'errorCode'=>'30007','msg'=>'修改失败','status'=>false,'debug'=>false]);
            }
        }
    }

}